package com.xiaofengstu.mcams.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaofengstu.mcams.util.ThreadLocalUtils;
import com.xiaofengstu.mcams.web.entity.TMenu;
import com.xiaofengstu.mcams.web.entity.TRole;
import com.xiaofengstu.mcams.web.entity.TUser;
import com.xiaofengstu.mcams.web.mapper.TMenuMapper;
import com.xiaofengstu.mcams.web.mapper.TRoleMapper;
import com.xiaofengstu.mcams.web.mapper.TUserMapper;
import com.xiaofengstu.mcams.web.service.TMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fengzeng
 * @since 2022-05-07
 */
@Service
@RequiredArgsConstructor
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu> implements TMenuService {

  private final TMenuMapper menuMapper;
  private final TRoleMapper roleMapper;
  private final TUserMapper userMapper;


  @Override
  public List<TMenu> getMenuList() {

    Integer userId = ThreadLocalUtils.get();
    TUser user = userMapper.selectById(userId);

    TRole tRole = roleMapper.selectOne(new QueryWrapper<TRole>()
        .eq("role_id", user.getRoleId()));

    String[] authIds = tRole.getRoleAuth().split(",");
    return getMenuList(authIds);
  }

  public List<TMenu> getMenuList(String[] authIds) {
    List<TMenu> menuList = menuMapper.selectList(new QueryWrapper<TMenu>().in("menu_id", authIds));
    List<TMenu> rootMenu = menuList.stream()
        .filter((x) -> x.getMenuPid() == 0)
        .collect(Collectors.toList());
    Map<Integer, TMenu> rootMenuMap = new HashMap<>(rootMenu.size());
    for (TMenu menu : rootMenu) {
      menu.setChildren(new ArrayList<>());
      rootMenuMap.put(menu.getMenuId(), menu);
    }

    for (TMenu menu : menuList) {
      if (rootMenuMap.containsKey(menu.getMenuPid())) {
        rootMenuMap.get(menu.getMenuPid()).getChildren().add(menu);
      }
    }
    return List.copyOf(rootMenuMap.values());
  }

  @Override
  public Integer removeByMenuId(Integer roleId, Integer menuId) {
    TRole role = roleMapper.selectById(roleId);
    String[] menuIds = role.getRoleAuth().split(",");
    for (int i = 0; i < menuIds.length; i++) {
      if (menuIds[i].equals(String.valueOf(menuId))) {
        menuIds[i] = null;
      }
    }


    StringBuilder sb = new StringBuilder();
    for (String s : menuIds) {
      if (s != null) {
        sb.append(s).append(",");
      }
    }
    role.setRoleAuth(sb.toString());
    return roleMapper.updateById(role);
  }

  @Override
  public List<TMenu> getByRoleId(Integer roleId) {
    TRole role = roleMapper.selectById(roleId);
    return getMenuList(role.getRoleAuth().split(","));

  }

  @Override
  public List<TMenu> getAllMenusTree() {
    List<TMenu> menuList = menuMapper.selectList(new QueryWrapper<>());
    String[] authIds = new String[menuList.size()];
    for (int i = 0; i < authIds.length; i++) {
      authIds[i] = menuList.get(i).getMenuId().toString();
    }
    return getMenuList(authIds);
  }

}
